package com.jugheadzhou.emrpoc.service.impl;

import com.jugheadzhou.emrpoc.common.EmrResultCode;
import com.jugheadzhou.emrpoc.common.exception.EmrBusinessException;
import com.jugheadzhou.emrpoc.dto.MedicalRecordDto;
import com.jugheadzhou.emrpoc.mapper.MedicalRecordMapper;
import com.jugheadzhou.emrpoc.mapper.MedicalRecordTemplateMapper;
import com.jugheadzhou.emrpoc.model.MedicalRecord;
import com.jugheadzhou.emrpoc.service.MedicalRecordService;
import com.jugheadzhou.emrpoc.vo.MedicalRecordTemplateVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 病历业务层
 *
 * @author jugheadzhou
 * @date 2021-10-24
 */
@Slf4j
@Service
public class MedicalRecordServiceImpl implements MedicalRecordService {
    @Resource
    private MedicalRecordMapper medicalRecordMapper;
    @Resource
    private MedicalRecordTemplateMapper medicalRecordTemplateMapper;

    /**
     * 病历保存
     *
     * @param medicalRecordDto 保存的病历对象
     * @return
     */
    @Override
    public int save(MedicalRecordDto medicalRecordDto) {
        // 验证
        MedicalRecordTemplateVo templateVo = medicalRecordTemplateMapper.selectVoById(medicalRecordDto.getTemplateId());
        if (templateVo == null) {
            throw new EmrBusinessException(EmrResultCode.MR_TP_NOT_HAS_PUBLISHED);
        }
        // 生成序列号，用于多例限流
        String currentSerialNumber = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
        String maxSerialNumber = medicalRecordMapper.selectMaxSerialNumberByVisitCardNoAndType(medicalRecordDto.getVisitCardNo(), medicalRecordDto.getType());
        // 多例情况下限流
        if (templateVo.getAllowMultiple() == 1) {
            if (currentSerialNumber.equals(maxSerialNumber)) {
                throw new EmrBusinessException(EmrResultCode.MR_MULTI_LIMIT);
            }
            // 单例情况下限量
        } else {
            if (StringUtils.isNotBlank(maxSerialNumber)) {
                throw new EmrBusinessException(EmrResultCode.MR_SINGLE_LIMIT);
            }
        }
        // 入库
        MedicalRecord toInsertMedicalRecord = new MedicalRecord();
        BeanUtils.copyProperties(medicalRecordDto, toInsertMedicalRecord);
        toInsertMedicalRecord.setCreatedBy("save user");
        toInsertMedicalRecord.setName(templateVo.getTitle());
        toInsertMedicalRecord.setCreateTime(LocalDateTime.now());
        toInsertMedicalRecord.setSerialNumber(currentSerialNumber);
        int count = medicalRecordMapper.insert(toInsertMedicalRecord);
        log.info("病历保存结束");
        return count;
    }

    /**
     * 病历查询
     *
     * @param medicalRecordDto
     * @return
     */
    @Override
    public List<MedicalRecord> list(MedicalRecordDto medicalRecordDto) {
        return medicalRecordMapper.list(medicalRecordDto);
    }
}
